/*
 * Copyright 2009 IT Mill Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.tor.tribes.reportserver;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.HttpServletRequestListener;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;
import de.tor.tribes.reportserver.ui.LoginScreen;
import de.tor.tribes.reportserver.ui.MainScreen;
import de.tor.tribes.reportserver.util.ZebraInitializer;
import de.tor.tribes.types.ZebraUser;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.Template;
import org.apache.velocity.app.VelocityEngine;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class MyVaadinApplication extends Application implements ILoginListener, HttpServletRequestListener {

    private Window window;
    private LoginScreen login;
    private MainScreen mainScreen;
    private PersistenceImpl persistence;
    private VelocityEngine vEngine;

    @Override
    public void init() {
        persistence = new PersistenceImpl();
        setTheme("zebra");
        try {
            ZebraInitializer.initialize(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        window = new Window("ReportServer");
        setMainWindow(window);
        login = new LoginScreen(this);
        window.setContent(login);
        window.getContent().setSizeFull();

        vEngine = new VelocityEngine();
        vEngine.setApplicationAttribute("javax.servlet.ServletContext", ((WebApplicationContext) getContext()).getHttpSession().getServletContext());
        vEngine.setProperty("resource.loader", "webapp");
        vEngine.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.servlet.WebappLoader");
        vEngine.setProperty("webapp.resource.loader.path", "/WEB-INF/tpl");
        vEngine.init();
    }

    public VelocityEngine getVelocity() {
        return vEngine;
    }

    public PersistenceImpl getPersistence() {
        return persistence;
    }

    public void fireLoginEvent(ZebraUser pUser) {
        if (pUser == null) {
            window.showNotification("Fehler", "Benutzer nicht gefunden oder Passwort falsch.", Notification.TYPE_ERROR_MESSAGE);
            return;
        }
        try {
            setUser(pUser);
            mainScreen = new MainScreen();
            window.setContent(mainScreen);
            window.getContent().setSizeFull();
            mainScreen.reload();
        } catch (Throwable t) {
            Logger.getLogger(MyVaadinApplication.class.getName()).log(Level.SEVERE, "Failed to login into Zebra", t);
            window.showNotification("Fehler", "Login fehlgeschlagen, bitte kontaktieren den Administrator.", Notification.TYPE_ERROR_MESSAGE);
            fireLogoutEvent();
        }
    }

    public void fireLogoutEvent() {
        setUser(null);
        mainScreen = null;
        persistence.close();
        window.setContent(login);
    }

    public void onRequestStart(HttpServletRequest request, HttpServletResponse response) {
        System.out.println(" Query string: " + request.getQueryString());
        /* if (getUser() != null) {
         mainScreen.reload();
         }*/
    }

    public void onRequestEnd(HttpServletRequest request, HttpServletResponse response) {
    }
}
